package com.tomecode.tools.maven.settings.gui;

import java.io.File;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.grouplayout.GroupLayout;
import org.eclipse.swt.layout.grouplayout.LayoutStyle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.tomecode.tools.maven.settings.config.Maven;
import com.tomecode.tools.maven.settings.config.MavenVersion;
import com.tomecode.tools.maven.settings.gui.images.ImageFactory;
import com.tomecode.tools.maven.settings.gui.utils.GuiUtils;
import com.tomecode.tools.maven.settings.gui.utils.SWTResourceManager;

/**
 * Frm for editing maven home and repository directory
 * 
 * @author Tomas Frastia
 * @see http://www.tomecode.com or
 *      http://code.google.com/p/maven-settings-editor/
 * 
 */
public final class FrmMaven extends Dialog {

	private Maven result;
	private Shell shlMavenDirectory;
	private Text textMavenHome;
	private Text textMavenRepository;
	private Button btnSave;
	private Label labelWarning;
	private Combo comboVersion;

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public FrmMaven(Shell parent) {
		super(parent, SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM);
		setText("Maven Configuration");
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public final Maven open(Maven exitsMaven) {
		createContents(exitsMaven);
		shlMavenDirectory.open();
		shlMavenDirectory.layout();
		Display display = getParent().getDisplay();
		while (!shlMavenDirectory.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents(Maven exitsMaven) {
		shlMavenDirectory = new Shell(getParent(), getStyle());
		shlMavenDirectory.setImage(ImageFactory.MAVEN_SETTINGS_EDITOR_16);
		shlMavenDirectory.setSize(730, 203);

		Composite composite = new Composite(shlMavenDirectory, SWT.NONE);

		btnSave = new Button(composite, SWT.NONE);
		if (exitsMaven == null) {
			this.result = new Maven();
			shlMavenDirectory.setText("New Maven configuration");
			btnSave.setEnabled(false);
		} else {
			this.result = exitsMaven;
			shlMavenDirectory.setText("Edit Maven configuration");
			btnSave.setEnabled(true);
		}

		Group group = new Group(composite, SWT.NONE);

		Label lblMaven = new Label(group, SWT.NONE);
		lblMaven.setFont(SWTResourceManager.getFont("Lucida Grande", 11, SWT.BOLD));

		lblMaven.setText("Maven Home");

		textMavenHome = new Text(group, SWT.BORDER);
		textMavenHome.setText(result.getHome() == null ? "" : result.getHome().toString());
		textMavenHome.addKeyListener(new KeyAdapter() {
			public final void keyReleased(KeyEvent e) {
				validate();
			}
		});

		Button buttonFindMaven = new Button(group, SWT.NONE);
		buttonFindMaven.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				DirectoryDialog dialog = new DirectoryDialog(shlMavenDirectory);
				dialog.setMessage("Select Maven home");
				dialog.setText("Select Maven home");
				String dir = dialog.open();
				if (dir != null) {
					textMavenHome.setText(dir);
					validate();
				}
			}
		});

		buttonFindMaven.setText("...");

		Label lblMavenRepository = new Label(group, SWT.NONE);
		lblMavenRepository.setFont(SWTResourceManager.getFont("Lucida Grande", 11, SWT.BOLD));
		// if (GuiUtils.isWindows()) {
		// lblMavenRepository.setBounds(10, 36, 127, 14);
		// }
		lblMavenRepository.setText("Maven Repository");

		textMavenRepository = new Text(group, SWT.BORDER);
		textMavenRepository.setText(result.getRepository() == null ? "" : result.getRepository().toString());
		textMavenRepository.addKeyListener(new KeyAdapter() {
			public final void keyReleased(KeyEvent e) {
				validate();
			}
		});
		if (GuiUtils.isWindows()) {
			textMavenRepository.setBounds(143, 33, 509, 19);
		}
		Button buttonFindRepo = new Button(group, SWT.NONE);
		buttonFindRepo.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				DirectoryDialog dialog = new DirectoryDialog(shlMavenDirectory);
				dialog.setMessage("Select Maven repository");
				dialog.setText("Select Maven repository");
				String dir = dialog.open();
				if (dir != null) {
					textMavenRepository.setText(dir);
					validate();
				}
			}
		});
		buttonFindRepo.setText("...");

		Button btnCancel = new Button(composite, SWT.NONE);
		btnCancel.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				shlMavenDirectory.close();
			}

		});

		btnCancel.setText("Cancel");

		btnSave.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				result.setHome(new File(textMavenHome.getText().trim()));
				result.setRepository(new File(textMavenRepository.getText().trim()));
				shlMavenDirectory.close();
			}
		});

		btnSave.setText("Save");

		labelWarning = new Label(composite, SWT.NONE);
		labelWarning.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		labelWarning.setFont(SWTResourceManager.getFont("Lucida Grande", 11, SWT.BOLD | SWT.ITALIC));
		GroupLayout gl_shlMavenDirectory = new GroupLayout(shlMavenDirectory);
		gl_shlMavenDirectory.setHorizontalGroup(gl_shlMavenDirectory.createParallelGroup(GroupLayout.LEADING).add(composite, GroupLayout.DEFAULT_SIZE, 724, Short.MAX_VALUE));
		gl_shlMavenDirectory.setVerticalGroup(gl_shlMavenDirectory.createParallelGroup(GroupLayout.LEADING).add(composite, GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE));
		GroupLayout gl_composite = new GroupLayout(composite);
		gl_composite.setHorizontalGroup(gl_composite
				.createParallelGroup(GroupLayout.TRAILING)
				.add(gl_composite.createSequentialGroup().add(10).add(labelWarning, GroupLayout.PREFERRED_SIZE, 698, GroupLayout.PREFERRED_SIZE).add(10))
				.add(gl_composite.createSequentialGroup().addContainerGap(516, Short.MAX_VALUE).add(btnSave, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE).addPreferredGap(LayoutStyle.RELATED)
						.add(btnCancel, GroupLayout.PREFERRED_SIZE, 106, GroupLayout.PREFERRED_SIZE).addContainerGap()).add(GroupLayout.LEADING, group, GroupLayout.DEFAULT_SIZE, 730, Short.MAX_VALUE));
		gl_composite.setVerticalGroup(gl_composite.createParallelGroup(GroupLayout.LEADING).add(
				gl_composite.createSequentialGroup().add(group, GroupLayout.PREFERRED_SIZE, 112, GroupLayout.PREFERRED_SIZE).addPreferredGap(LayoutStyle.RELATED).add(labelWarning, GroupLayout.PREFERRED_SIZE, 14, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(LayoutStyle.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.add(gl_composite.createParallelGroup(GroupLayout.BASELINE).add(btnCancel, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE).add(btnSave, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)).addContainerGap()));

		comboVersion = new Combo(group, SWT.NONE | SWT.READ_ONLY);
		comboVersion.setItems(MavenVersion.getValues());
		if (result.getVersion() == MavenVersion.UNKNOWN) {
			comboVersion.select(0);
		} else if (result.getVersion() == MavenVersion.V221) {
			comboVersion.select(1);
		} else if (result.getVersion() == MavenVersion.V30) {
			comboVersion.select(2);
		}

		validate();
		comboVersion.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				result.setVersion(comboVersion.getItem(comboVersion.getSelectionIndex()));
				validate();
			}
		});

		Label lblVersion = new Label(group, SWT.NONE);
		lblVersion.setText("Version");
		lblVersion.setFont(SWTResourceManager.getFont("Lucida Grande", 11, SWT.BOLD));
		GroupLayout gl_group = new GroupLayout(group);
		gl_group.setHorizontalGroup(gl_group.createParallelGroup(GroupLayout.LEADING).add(
				gl_group.createSequentialGroup()
						.addContainerGap()
						.add(gl_group
								.createParallelGroup(GroupLayout.LEADING)
								.add(gl_group.createSequentialGroup().add(lblVersion, GroupLayout.PREFERRED_SIZE, 127, GroupLayout.PREFERRED_SIZE).add(6).add(comboVersion, GroupLayout.PREFERRED_SIZE, 509, GroupLayout.PREFERRED_SIZE))
								.add(gl_group.createSequentialGroup().add(lblMaven, GroupLayout.PREFERRED_SIZE, 127, GroupLayout.PREFERRED_SIZE).add(6).add(textMavenHome, GroupLayout.PREFERRED_SIZE, 509, GroupLayout.PREFERRED_SIZE).add(6)
										.add(buttonFindMaven, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE))
								.add(gl_group.createSequentialGroup().add(lblMavenRepository, GroupLayout.PREFERRED_SIZE, 127, GroupLayout.PREFERRED_SIZE).add(6).add(textMavenRepository, GroupLayout.PREFERRED_SIZE, 509, GroupLayout.PREFERRED_SIZE).add(6)
										.add(buttonFindRepo, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE))).addContainerGap(36, Short.MAX_VALUE)));
		gl_group.setVerticalGroup(gl_group.createParallelGroup(GroupLayout.LEADING).add(
				GroupLayout.TRAILING,
				gl_group.createSequentialGroup()
						.addContainerGap(24, Short.MAX_VALUE)
						.add(gl_group.createParallelGroup(GroupLayout.LEADING).add(gl_group.createSequentialGroup().add(4).add(lblVersion, GroupLayout.PREFERRED_SIZE, 14, GroupLayout.PREFERRED_SIZE))
								.add(comboVersion, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.add(6)
						.add(gl_group.createParallelGroup(GroupLayout.LEADING).add(gl_group.createSequentialGroup().add(4).add(lblMaven, GroupLayout.PREFERRED_SIZE, 14, GroupLayout.PREFERRED_SIZE))
								.add(gl_group.createSequentialGroup().add(1).add(textMavenHome, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).add(buttonFindMaven, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE))
						.add(3)
						.add(gl_group.createParallelGroup(GroupLayout.LEADING).add(gl_group.createSequentialGroup().add(4).add(lblMavenRepository, GroupLayout.PREFERRED_SIZE, 14, GroupLayout.PREFERRED_SIZE))
								.add(gl_group.createSequentialGroup().add(1).add(textMavenRepository, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).add(buttonFindRepo)).addContainerGap()));
		group.setLayout(gl_group);
		composite.setLayout(gl_composite);
		shlMavenDirectory.setLayout(gl_shlMavenDirectory);

	}

	private final void validate() {
		String item = comboVersion.getItem(comboVersion.getSelectionIndex());
		if (!MavenVersion.isSupported(item)) {
			btnSave.setEnabled(false);
			return;
		} else if (textMavenHome.getText().trim().length() == 0) {
			btnSave.setEnabled(false);
			return;
		} else {
			File f = new File(textMavenHome.getText().trim());
			f = new File(f.getPath() + File.separator + "conf" + File.separator + "settings.xml");
			if (!f.exists() || !f.isFile()) {
				btnSave.setEnabled(false);
				return;
			}
		}
		if (textMavenHome.getText().trim().length() == 0) {
			btnSave.setEnabled(false);
			return;
		} else {
			File f = new File(textMavenRepository.getText().trim());
			if (!f.exists() || !f.isDirectory()) {
				btnSave.setEnabled(false);
				return;
			}
		}
		btnSave.setEnabled(true);
	}
}
